package com.ruoyi.report_lqhhl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_lqhhl.domain.bo.ReportLqhhlDwddjlbBsDetailBo;
import com.ruoyi.report_lqhhl.domain.vo.ReportLqhhlDwddjlbBsDetailVo;
import com.ruoyi.report_lqhhl.domain.ReportLqhhlDwddjlbBsDetail;
import com.ruoyi.report_lqhhl.mapper.ReportLqhhlDwddjlbBsDetailMapper;
import com.ruoyi.report_lqhhl.service.IReportLqhhlDwddjlbBsDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 沥青混合料动稳定度试验检测记录详情Service业务层处理
 *
 * @author llb
 * @date 2024-08-19
 */
@RequiredArgsConstructor
@Service
public class ReportLqhhlDwddjlbBsDetailServiceImpl implements IReportLqhhlDwddjlbBsDetailService {

    private final ReportLqhhlDwddjlbBsDetailMapper baseMapper;

    /**
     * 查询沥青混合料动稳定度试验检测记录详情
     */
    @Override
    public ReportLqhhlDwddjlbBsDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询沥青混合料动稳定度试验检测记录详情列表
     */
    @Override
    public TableDataInfo<ReportLqhhlDwddjlbBsDetailVo> queryPageList(ReportLqhhlDwddjlbBsDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportLqhhlDwddjlbBsDetail> lqw = buildQueryWrapper(bo);
        Page<ReportLqhhlDwddjlbBsDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询沥青混合料动稳定度试验检测记录详情列表
     */
    @Override
    public List<ReportLqhhlDwddjlbBsDetailVo> queryList(ReportLqhhlDwddjlbBsDetailBo bo) {
        LambdaQueryWrapper<ReportLqhhlDwddjlbBsDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportLqhhlDwddjlbBsDetail> buildQueryWrapper(ReportLqhhlDwddjlbBsDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportLqhhlDwddjlbBsDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, ReportLqhhlDwddjlbBsDetail::getExcelId, bo.getExcelId());
        lqw.eq(bo.getBianxlt1() != null, ReportLqhhlDwddjlbBsDetail::getBianxlt1, bo.getBianxlt1());
        lqw.eq(bo.getBianxlt2() != null, ReportLqhhlDwddjlbBsDetail::getBianxlt2, bo.getBianxlt2());
        lqw.eq(bo.getT1() != null, ReportLqhhlDwddjlbBsDetail::getT1, bo.getT1());
        lqw.eq(bo.getT2() != null, ReportLqhhlDwddjlbBsDetail::getT2, bo.getT2());
        lqw.eq(bo.getDongwdd() != null, ReportLqhhlDwddjlbBsDetail::getDongwdd, bo.getDongwdd());
        return lqw;
    }

    /**
     * 新增沥青混合料动稳定度试验检测记录详情
     */
    @Override
    public Boolean insertByBo(ReportLqhhlDwddjlbBsDetailBo bo) {
        ReportLqhhlDwddjlbBsDetail add = BeanUtil.toBean(bo, ReportLqhhlDwddjlbBsDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改沥青混合料动稳定度试验检测记录详情
     */
    @Override
    public Boolean updateByBo(ReportLqhhlDwddjlbBsDetailBo bo) {
        ReportLqhhlDwddjlbBsDetail update = BeanUtil.toBean(bo, ReportLqhhlDwddjlbBsDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportLqhhlDwddjlbBsDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除沥青混合料动稳定度试验检测记录详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
